package gui;

import application.Resolution;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Fenetre extends JFrame {

    private final int LARGEUR_INITIALE = 400;
    private final int HAUTEUR_INITIALE = 300;
    private Fenetre fenetre;
    private Jeu jeu;
    private Resolution resolution;
    private JButton btnLaunch1080;
    private JButton btnLaunch1024;
    private JButton btnLaunch600;
    private JButton btnQuitter;

    public Fenetre() {
        setSize(LARGEUR_INITIALE, HAUTEUR_INITIALE);

        fenetre = this;

        setUndecorated(true);
        setResizable(false);
        setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2);
        setLayout(null);

        initialisation();

        setVisible(true);
    }

    public void setResolution(Resolution resolution) {
        this.resolution = resolution;
    }

    private void initialisation() {
        setCurseur();

        setupBoutonsIni();

    }
    
    /**
     * Configure le curseur pour le rendre personnalise
     */
    private void setCurseur() {
        Cursor curseur;
        Image imageCurseur = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("images/Curseur.png"));
        Point pointCurseur = new Point(11, 0);
        curseur = Toolkit.getDefaultToolkit().createCustomCursor(imageCurseur, pointCurseur, "Curseur");

        setCursor(curseur);
    }

    private void setupBoutonsIni() {
        //Bouton launch 1080p
        btnLaunch1080 = new JButton("Lancer en 1920x1080");
        btnLaunch1080.setSize(getWidth(), getHeight() / 4);
        btnLaunch1080.setLocation(0, 0);
        btnLaunch1080.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fenetre.remove(btnLaunch1024);
                fenetre.remove(btnLaunch1080);
                fenetre.remove(btnLaunch600);
                fenetre.remove(btnQuitter);

                fenetre.resolution = Resolution.R1920x1080;
                fenetre.setSize(fenetre.resolution.getLargeur(), fenetre.resolution.getHauteur());
                fenetre.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2);

                //setJMenuBar(barBarreMenu);
                jeu = new Jeu(fenetre, resolution);
                fenetre.add(jeu);
                fenetre.repaint();
            }
        });
        add(btnLaunch1080);

        //Bouton launch 1280x1024
        btnLaunch1024 = new JButton("Lancer en 1280x1024");
        btnLaunch1024.setSize(getWidth(), getHeight() / 4);
        btnLaunch1024.setLocation(0, getHeight() / 4);
        btnLaunch1024.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fenetre.remove(btnLaunch1024);
                fenetre.remove(btnLaunch1080);
                fenetre.remove(btnLaunch600);
                fenetre.remove(btnQuitter);

                fenetre.resolution = Resolution.R1280x1024;
                fenetre.setSize(fenetre.resolution.getLargeur(), fenetre.resolution.getHauteur());
                fenetre.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2);

                //setJMenuBar(barBarreMenu);
                jeu = new Jeu(fenetre, resolution);
                fenetre.add(jeu);
                fenetre.repaint();
            }
        });
        add(btnLaunch1024);

        //Bouton launch 800x600
        btnLaunch600 = new JButton("Lancer en 800x600");
        btnLaunch600.setSize(getWidth(), getHeight() / 4);
        btnLaunch600.setLocation(0, getHeight() / 2);
        btnLaunch600.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fenetre.remove(btnLaunch1024);
                fenetre.remove(btnLaunch1080);
                fenetre.remove(btnLaunch600);
                fenetre.remove(btnQuitter);

                fenetre.resolution = Resolution.R800x600;
                fenetre.setSize(fenetre.resolution.getLargeur(), fenetre.resolution.getHauteur());
                fenetre.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2);


                //setJMenuBar(barBarreMenu);
                jeu = new Jeu(fenetre, resolution);
                fenetre.add(jeu);
                fenetre.repaint();
            }
        });
        add(btnLaunch600);

        //Bouton quitter launcher
        btnQuitter = new JButton("Quitter");
        btnQuitter.setSize(getWidth(), getHeight() / 4);
        btnQuitter.setLocation(0, 3 * getHeight() / 4);
        btnQuitter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        add(btnQuitter);
    }

    public void refresh(Resolution resolutionInitiale) {
        setSize(resolution.getLargeur(), resolution.getHauteur());
        setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - getWidth() / 2, Toolkit.getDefaultToolkit().getScreenSize().height / 2 - getHeight() / 2);

        jeu.refresh(resolutionInitiale);
    }
}
